import { Component } from "@angular/core";
import { NavController, NavParams, AlertController, ToastController, Platform, LoadingController } from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import {
  MediaCapture,
  MediaFile,
  CaptureError,
  CaptureImageOptions,
  CaptureAudioOptions,
  CaptureVideoOptions
} from '@ionic-native/media-capture';
import { appConfig } from "../../../providers/config";
import { HomePage } from "../../home/home";

@Component({
  templateUrl: "record-media.html"
})
export class RecordMedia {

  buttonwz = "完成";
  type = null;
  checkNameUrl: any = null;
  checkerId: any = null;
  checkerName: any = null;
  constructor(
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private httpService: HttpService,
    private storage: Storage,
    private navCtrl: NavController,
    public alertCtrl: AlertController,
    private mediaCapture: MediaCapture,
    private toast: ToastController,
    private platform: Platform,
    private loading: LoadingController
  ) {
    this.storage.get("user").then(data => {
      this.checkerId = data.id;
      this.checkerName = data.userName;
    });
  }
  ionViewWillEnter() {

  }

  doCamera() {
    let options: CaptureImageOptions = { limit: 1 };
    this.mediaCapture.captureImage(options).then(
      (data: MediaFile[]) => {
        let path = data[0].fullPath;
        let message = "已保存在您手机的相册中";
        let tt = this.toast.create({
          message: message,
          duration: 4000,
          position: "top"
        });
        tt.present();
        this.type = "1";
        this.buttonwz = "完成";
      },
      (err: CaptureError) => {
        console.error(err);
      }
    );
  }

  doAudio() {
    let options: CaptureAudioOptions = { limit: 1, duration: 6000 };
    this.mediaCapture.captureAudio(options).then(
      (data: MediaFile[]) => {
        let path = data[0].fullPath;
        let message = "已保存在您手机的录音中";
        if (this.platform.is("ios")) {
          message = "已保存在您手机的语音备忘录中";
        }
        let tt = this.toast.create({
          message: message,
          duration: 4000,
          position: "top"
        });
        tt.present();
        this.type = "2";
        this.buttonwz = "完成";
      },
      (err: CaptureError) => {
        console.error(err);
      }
    );
  }

  doVideo() {
    let options: CaptureVideoOptions = { limit: 1, quality: 0 }; //quality 默认是1 高分辨率，0，低分辨率
    this.mediaCapture.captureVideo(options).then(
      (data: MediaFile[]) => {
        let path = data[0].fullPath;
        let message = "已保存在您手机的相册中";
        let tt = this.toast.create({
          message: message,
          duration: 4000,
          position: "top"
        });
        tt.present();
        this.type = "3";
        this.buttonwz = "完成";
      },
      (err: CaptureError) => {
        console.error(err);
      }
    );
  }

  done() {
    let load = this.loading.create({
      spinner: appConfig.spinner,
      duration: appConfig.timeout
    });
    load.present();
    let param = {
      checkNameUrl: this.checkNameUrl,
      checkerId: this.checkerId,
      checkerName: this.checkerName,
      pDetailId: this.navParams.get("pdId")
    }
    this.httpService.updateSafeContrCheck(param).subscribe(data => {
      load.dismiss();
      if (data.status == 200) {
        let tt = this.toast.create({
          message: "提交成功",
          duration: 2000,
          position: "top"
        });
        tt.present().then(() => {
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            this.navCtrl
              .getActive()
              .getNav()
              .remove(0);
          });
        });
      } else {
        const alert = this.alertCtrl.create({
          subTitle: data.message,
          buttons: ["确定"]
        });
        alert.present();
      }
    })
  }

  getImgUrl(param) {
    this.checkNameUrl = param.imageDate;
  }

}
